<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"
	/>
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no" />
	<script src="/javascripts/jquery-3.3.1.min.js"></script>
	<script src="/javascripts/ydui.flexible.js"></script>
	<script src="/javascripts/ydui.js"></script>

	<script type="text/javascript" src="/layer/layer.js"></script>
	<link rel="stylesheet" type="text/css" href="/stylesheets/style.css" />
	<title>IOV 空投福利</title>
</head>

<body>
	<img src="/images/contBg1.png" />
	<section class="contentpadd">
		<div class="m-cell">
			<div class="cell-item">
				<div class="cell-right">
					<input type="text" id="address" class="cell-input" placeholder="请输入ETH钱包地址" autocomplete="off" />
				</div>
			</div>
		</div>
		<div class="m-cell">
			<div class="cell-item">
				<div class="cell-right">
					<input type="text" id="wxname" class="cell-input" placeholder="请输入微信名字" autocomplete="off" />
				</div>
			</div>
		</div>
		<div class="m-cell">
			<div class="cell-item">
				<div class="cell-right">
					<input type="text" id="Invitationcode" class="cell-input" placeholder="请输入邀请码" autocomplete="off" value="<%= code %>"></input>
				</div>
			</div>
		</div>
		<div class="m-cell">
			<div class="cell-item">
				<div class="cell-right">
					<input type="text" id="authcode" class="cell-input" placeholder="请输入验证码" autocomplete="off" />
				</div>
				<div class="cell-right" style="padding-right: 0; width: 60px">
					<img id="captcha" onclick="changecode()" style="width: 100px" height="50" src="/captcha" />
				</div>
			</div>
		</div>
		<div class="">
			<a href="javascript:;" type="button" onclick="submitBtn()" class="btn-block btn-primary">领取30 IOV</a>
		</div>
	</section>
	<div class="bottom_link">
		<img src="/images/contBg2.png" />
		<h3 class="myyq" id="J_Confirm">我的邀请</h3>
		<a href="http://www.carlive.io/iov/zh/" target="_blank" class="linktext">http://www.carlive.io/iov/zh/</a>
	</div>
</body>

</html>
<script type="text/javascript">
	/* 获取头部信息 */
	function GetQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) {
			return r[2];
		}
	};
	var inviteCode = GetQueryString('inviteCode');
	console.log(inviteCode);
	$('#J_Confirm').on('click', function () {
		layer.prompt({ title: '请输入您的ETH钱包地址', formType: 2 }, function (value, text, index) {
			console.log(value);
			window.location.href = '/generalize?eth=' + value;
			layer.close(index);
		});
	});
	//声明
	var address, wxname, Invitationcode, authcode;
	if (inviteCode) {
		$('#Invitationcode').val(inviteCode);
	}
	// 更换验证码
	function changecode() {
		$.ajax({
			url: '/captcha',
			type: 'GET',
		}).done(function (res) {
			if (res) {
				$("#captcha").attr("src", "data:image/svg+xml;utf8," + XMLtoString(res));
			}
		}).fail(function (res) { layer.msg("网络错误/服务器异常"); });
	}
	//提交验证
	function submitBtn() {
		var address = $("#address").val();
		var wxname = $("#wxname").val();
		var Invitationcode = $("#Invitationcode").val();
		var authcode = $("#authcode").val();

		if (address == "") {
			layer.msg("请输入ETH钱包地址");
			return;
		} else if (wxname == "") {
			layer.msg("请输入微信名字");
			return;
			// } else if (Invitationcode == "") {
			// 	layer.msg("请输入请输入邀请码");
			// 	return;
		} else if (authcode == "") {
			layer.msg("请输入验证码");
			return;
		}
		var postData = { eth: address, wechatName: wxname, inviteCode: Invitationcode, code: authcode };
		$.ajax({
			url: '/users/add',
			type: 'POST',
			data: postData,
		}).done(function (res) {
			if (res && res.code == 0) {
				layer.msg("提交成功");
				// var url = '/generalize?code='+res.data;
				window.location.href = '/generalize?code=' + res.data + '&eth=' + postData.eth;
				// window.location.href(url);
			} else {
				layer.msg(res.msg);
			}
		}).fail(function (res) { layer.msg("网络错误/服务器异常"); });
	}

	function XMLtoString(elem) {
		var serialized;
		try {
			// XMLSerializer exists in current Mozilla browsers 
			serializer = new XMLSerializer();
			serialized = serializer.serializeToString(elem);
		}
		catch (e) {
			// Internet Explorer has a different approach to serializing XML 
			serialized = elem.xml;
		}
		return serialized;
	}
</script>